JavaScript Data Structures and Algorithms by Sammie Bae

JavaScript Data Structures and Algorithms by Sammie Bae
Author: Sammie Bae
Language: eng
Format: epub, pdf
ISBN: 9781484239889
Publisher: Apress

Time Complexity: O(1)

Insertion


Inserting into a stack can be done via the push function natively supported with JavaScript arrays.1 Stack.prototype.push = function(value){

2 this.array.push(value);

3 }

4

5 stack1.push(1);

6 stack1.push(2);

7 stack1.push(3);

8 console.log(stack1); // {array: [1,2,3]}

Time Complexity: O(1)

Deletion


Deletion can also be implemented using a native JavaScript array method, called pop.1 Stack.prototype.pop = function() {

2 return this.array.pop();

3 };

4

5 stack1.pop(1);

6 stack1.pop(2);

7 stack1.pop(3);

8

9 console.log(stack1); // {array: []}